﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Markup;
using Microsoft.Advertising.Mobile.UI;
using Common.Library;
using System.Text;
using System.Xml.Linq;
using OnlineVideos.Library;
using OnlineVideos.View_Models;
using OnlineVideos.Data;
using OnlineVideos.Utilities;
using Common.Utilities;

namespace OnlineVideos.Views
{
    public partial class CheatCodePage : PhoneApplicationPage
    {
       
        string xaml = string.Empty;
        List<GameCheats> GameCheatCodes = null;
      
    

        public CheatCodePage()
        {
            InitializeComponent();
            GameCheatCodes = new List<GameCheats>();
            Loaded += new RoutedEventHandler(CheatCodePage_Loaded);
        }
        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            try
            {
                FlurryWP8SDK.Api.EndTimedEvent("CheatCodePage");
            }
            catch (Exception ex)
            {

                Exceptions.SaveOrSendExceptions("Exception in OnNavigatedFrom Method In CheatCodePage.cs file.", ex);
            }
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            try
            {
                FlurryWP8SDK.Api.LogPageView();
            }
            catch (Exception ex)
            {

                Exceptions.SaveOrSendExceptions("Exception in OnNavigatedTo Method In CheatCodePage.cs file.", ex);
            }
        }
        void CheatCodePage_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                FlurryWP8SDK.Api.LogEvent("CheatCodePage", true);
                string id = "";
                string pid = "";
                if (NavigationContext.QueryString.TryGetValue("pid", out pid))
                {
                    if (NavigationContext.QueryString.TryGetValue("id", out id))

                        LayoutRoot.Background = ShowDetail.LoadShowPivotBackground(Convert.ToInt32(id));

                    GameCheatCodes = LoadCheatCode(pid);
                    if (GameCheatCodes.Count > 0)
                    {
                        tblkcheatcode.Visibility = Visibility.Collapsed;
                        lbxdes.ItemsSource = GameCheatCodes;
                    }
                    else
                    {
                        tblkcheatcode.Visibility = Visibility.Visible;
                    }
                }
            }
            catch (Exception ex)
            {
                
              Exceptions.SaveOrSendExceptions("Exception in CheatCodePage_Loaded Method In CheatCodePage.cs file.", ex);
            }
        }

        private List<GameCheats> LoadCheatCode(string pid)
        {
            
            OnlineVideoDataContext context = new OnlineVideoDataContext(Constants.DatabaseConnectionString);
            IQueryable<GameCheats> CheatCodesList = from k in context.ShowCheats where k.CheatID == Convert.ToInt32(pid) select k;
            foreach (var item in CheatCodesList)
            {
                GameCheats objvideoprop = new GameCheats();

                objvideoprop.CheatName = item.CheatName;
                if (item.CheatData == null)
                {
                    objvideoprop.CheatDescription = item.CheatDescription;
                    GameCheatCodes.Add(objvideoprop);
                }
                else
                {
                    xaml = @"<DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
                                    <Grid Margin=""0,0,0,15"">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width=""220""/>
                                        <ColumnDefinition Width=""*""/>
                                    </Grid.ColumnDefinitions>
                                <TextBlock x:Name=""txtblkfirstcolumn"" Grid.Column=""0"" Grid.Row=""1"" TextWrapping=""Wrap""  FontSize=""28"" Text=""{Binding FirstColumn}""/>
                                <TextBlock x:Name=""txtblksecondcolumn"" Grid.Column=""1"" Grid.Row=""1"" TextWrapping=""Wrap"" FontSize=""28"" Text=""{Binding SecondColumn}""/>
                          </Grid>
                        </DataTemplate>";
                    var dt = (DataTemplate)XamlReader.Load(xaml);
                    lbxdes.ItemTemplate = dt;

                    byte[] bytes = item.CheatData;
                    string dtacheat = UTF8Encoding.UTF8.GetString(bytes,0,bytes.Length);
                    XDocument xdoc = new XDocument();
                    xdoc = XDocument.Parse(dtacheat);
                    var query = from a in xdoc.Descendants("tr") select a;
                    foreach (var data in query.Elements())
                    {
                        if (data.Attribute("ri").Value != string.Empty)
                        {
                            GameCheats objvideoprop1 = new GameCheats();
                            objvideoprop1.FirstColumn = data.Attribute("ri").Value;
                            objvideoprop1.SecondColumn = data.Attribute("Ci").Value;
                            GameCheatCodes.Add(objvideoprop1);
                        }
                    }
                        
                }
            }

            return GameCheatCodes;
        }
    }
}
